/* global  expect, describe, it, apiTest */

const Role = require('../role/fn')
const Admin = require('../admin/fn')

const powerTree = [{
  code: 'app',
  title: '平台',
  child: [{
    code: 'appList',
    title: '应用列表',
    api: '/app/find',
    child: [
      { code: 'appCreate', title: '创建应用', api: '/app/create', method: 'post' },
      { code: 'appEnable', title: '暂停/恢复应用', api: '/app/enable', method: 'post' },
      { code: 'appEdit', title: '编辑应用', api: '/app/update', method: 'post' },
      { code: 'appPush', title: '应用回调配置', api: '/app/update', method: 'post', post: { hooks: ['123'] }},
      { code: 'appApi', title: '应用服务配置', api: '/app/update', method: 'post', post: { apis: ['123'] }}
    ]
  }, {
    code: 'staffList',
    title: '职员',
    api: '/staff/find'
  }]
}, {
  code: 'power',
  title: '权限',
  child: [{
    code: 'adminList',
    title: '管理员',
    api: '/admin/find',
    child: [
      { code: 'adminShow', title: '查看管理员', api: '/admin/findOne' },
      { code: 'adminCreate', title: '新增管理员', api: '/admin/create', method: 'post' },
      { code: 'adminEdit', title: '编辑管理员', api: '/admin/update', method: 'post' },
      { code: 'adminPower', title: '管理员角色分配', api: '/admin/update', method: 'post', post: { role: ['123'] }},
      { code: 'adminRemove', title: '删除管理员', api: '/admin/remove', method: 'post' }
    ]
  }, {
    code: 'roleList',
    title: '角色',
    api: '/role/find',
    child: [
      { code: 'roleCreate', title: '创建角色', api: '/role/create', method: 'post' },
      { code: 'roleShow', title: '查看角色', api: '/role/findOne' },
      { code: 'roleEdit', title: '编辑角色', api: '/role/update', method: 'post' },
      { code: 'roleRemove', title: '删除角色', api: '/role/remove', method: 'post' }
    ]
  }]
}, {
  code: 'log',
  title: '日志',
  child: [
    { code: 'logLogin', title: '登录日志', api: '/log/find', get: { type: 'login' }},
    { code: 'logAdmin', title: '后台日志', api: '/log/find', get: { type: 'admin' }},
    { code: 'logPush', title: '接受日志', api: '/log/find', get: { type: 'push' }},
    { code: 'logApi', title: '接口日志', api: '/log/find', get: { type: 'api' }}
  ]
}]

const treeArr = (arr) => {
  var data = []
  arr.forEach((one) => {
    if (one.api) data.push({ code: one.code, title: one.title, api: one.api, get: one.get, post: one.post, method: one.method })
    if (one.child) data.push.apply(data, treeArr(one.child))
  })
  return data
}
const powers = treeArr(powerTree)

let role = {}
let user = {}
let account = {}
let token = ''

describe('权限 - 集成测试', function() {
  it('初始化测试数据 |新增角色>新增管理员', function() {
    // 增
    var post = Role.randOne()
    post.powers = ['index']
    return Role.addOne(post, (data) => {
      role = data
      post = Admin.randOne()
      post.role = role._id
      account = post
      return Admin.addOne(post, (data) => {
        user = data
      })
    })
  })

  powers.splice(0).forEach((power) => {
    // if (!power.post) return
    it('设置权限| ' + power.title, function() {
      return Role.editOne(role._id, { powers: [power.code] }, (rs) => {
        // console.log(rs.powers)
        expect(rs.powers).to.include(power.code)
      })
    })
    it('登录账户| ' + power.title, function() {
      return apiTest.post('/account/login', {
        username: account.username, password: account.password
      }).then(function(res) {
        var data = apiTest.apiOk(res)
        token = res.data.token
        expect(data.role.powers).to.include(power.code, data.role.powers.join(','))
      })
    })
    it('验证权限| ' + power.title, function() {
      // console.log(power.code, token)
      let url = power.api
      let data = { token: token }
      if (power.method === 'post') {
        url = power.api + '?token=' + token
        data = power.post || {}
        // console.log(data, power.post, 1111)
      } else {
        power.method = 'get'
        Object.assign(data, power.get || {})
      }
      return apiTest[power.method](url, data).then(function(res) {
        console.log(power.code, power.api, res.data.code, data)
        expect(res.data.code).to.not.equal(30003, ['权限错误:', power.title, power.code, power.api].join(' '))
      })
    })
  })

  it('清理测试数据| 删除角色', function() {
    return Role.removeOne(role._id)
  })
  it('清理测试数据| 删除管理员', function() {
    return Admin.removeOne(user._id)
  })
})
